package cn.jobstart.spider.pipeline;

import cn.jobstart.common.StringUtil;
import cn.jobstart.enums.TalentSubscriptionTaskStatusEnum;
import cn.jobstart.spider.model.TalentJob51;
import cn.jobstart.spider.model.TalentJob51BaseInfo;
import cn.jobstart.spider.model.TalentLiePin;
import cn.jobstart.spider.parsing.TalentJob51Parsing;
import cn.jobstart.spider.parsing.TalentLiePinParsing;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.FutureTask;

/**
 * @author sunke
 * @version 1.0
 * @Description 51job 爬到数据后，处理
 * @date 2023-09-19 15:41
 */
public class TalentLiePinPipline extends AbstractPipline {










    /**
     * Process extracted results.
     *
     * @param resultItems resultItems
     * @param task        task
     */
    @Override
    public void process(ResultItems resultItems, Task task) {

        //本轮爬到的数据
        List<TalentLiePin> talentList = resultItems.get("talentList");
        //那个人才评阅
        String talentSubscriptionTaskId = resultItems.get("talentSubscriptionTaskId");

        if (StringUtil.isNullOrEmpty(talentList)) {
            //说明结束
           getTalentSubscriptionTaskService().updateStatus(TalentSubscriptionTaskStatusEnum.STOP.getType(), talentSubscriptionTaskId);


            return;
        }

        //更新爬取的总数量
        getTalentSubscriptionTaskService().updateResumeGraspCountByTaskIdAndCount(talentSubscriptionTaskId,talentList.size());

        List<FutureTask<Integer>> futureList = new ArrayList<>();


        talentList.stream().forEach(item -> {

            FutureTask<Integer> futureTask = new FutureTask<Integer>(() ->{

                doRun(item, talentSubscriptionTaskId);

                return 1;

            });

            futureList.add(futureTask);
            executor.submit(futureTask);


          //  executor.submit(() -> doRun(talentJob51Item, talentSubscriptionTaskId));


        });

      //  talentSubscriptionTaskService.updateStatus(TalentSubscriptionTaskStatusEnum.RUNNING.getType(), talentSubscriptionTaskId);

        //保证这一轮都结束
        futureList.stream().forEach(item->{

            try {
                item.get();
            } catch (Exception e) {
                e.printStackTrace();
            }

        });


      //  talentSubscriptionTaskService.updateStatus(TalentSubscriptionTaskStatusEnum.STOP.getType(), talentSubscriptionTaskId);
        futureList.clear();



    }




    private void doRun(TalentLiePin itemLiePin, String talentSubscriptionTaskId) {

        try {

          //  talentSubscriptionTaskService.updateStatus(TalentSubscriptionTaskStatusEnum.START.getType(), talentSubscriptionTaskId);


            if (getParsing() == null) {
               setParsing(new TalentLiePinParsing(talentSubscriptionTaskId));
            }

            getResumeEsService().insertAll(getParsing().parsing(itemLiePin));


        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
